package com.bcu.calculus.utils;

import com.bcu.calculus.entity.Message;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

public class FileUtil {

    private static int a = 5;

        /**
         * 判断指定的文件删除是否成功
         * @param fileName 文件路径
         * @return true or false 成功返回true，失败返回false
         */
        public static boolean deleteFile(String fileName){


            File file = new File(fileName);//根据指定的文件名创建File对象

            if (  file.exists() && file.isFile() ){ //要删除的文件存在且是文件

                if ( file.delete()){
                    System.out.println("文件"+fileName+"删除成功！");
                    return true;
                }else{
                    System.out.println("文件"+fileName+"删除失败！");
                    return false;
                }
            }else{

                System.out.println("文件"+fileName+"不存在，删除失败！" );
                return false;
            }


        }

    public static void copy(String filename, String oldpath, String newpath) throws IOException {
                 // TODO Auto-generated method stub
                 File oldpaths = new File(oldpath + "/" + filename);
                 File newpaths = new File(newpath + "/" + filename);
                 if (!newpaths.exists()) {
                       Files.copy(oldpaths.toPath(), newpaths.toPath());
                     } else {
                         newpaths.delete();
                         Files.copy(oldpaths.toPath(), newpaths.toPath());
                     }
             }

    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }


}
